package PlayerStates;

import java.util.Vector;

import Cards.Card;
import Cards.DealCard;
import Cards.MailCardBill;
import GameFiles.Days;
import GameFiles.StateBase;
import Gui.GameFrame;
import Gui.GamePanel;
import Main.Defines;
import Gui.Dialogs.DealCardDialog;
import Gui.Dialogs.EventDayDialog;
import Gui.Dialogs.LotteryDayDialog;
import Gui.Dialogs.MailCardBillDialog;
import Gui.Dialogs.PayDayDialog;
import Gui.Dialogs.SellDealCardDialog;

public class HandleDay extends StateBase {

	private static HandleDay ref;

	private HandleDay() {
	}

	public static HandleDay getInstance() {
		if (ref == null) {
			ref = new HandleDay();
		}
		return ref;
	}

	@Override
	public boolean Draw(GameFrame gf) {
		GamePanel.getInstance().update();
		GameFrame.getInstance().add(GamePanel.getInstance());
		GameFrame.getInstance().pack();
		return false;
	}

	@Override
	public boolean Enter() {
		GameFrame.getInstance().update();
		// Look at Days class for enum listing
		// Change this switch to target specific days no matter what the player
		// is on (to debug)
		int day = Defines.GM.getDayList().elementAt(Defines.CP().getDay())
		.getDayType();
		Card DrawnCard = null;
		System.out.printf("%s\n", Defines.GM.getDayList().elementAt(Defines.CP().getDay()).getDayName());
		

		switch (day) {
		case Days.STARTDAY:
			break;
		case Days.MAILDAY: {
			// TODO look here plz
			// we don't have to have separate mailcards
			// basically a find a buyer is always handled at the turn and a
			// player never has to actually at that mail card into their
			// list.
			// since each mail card has a name, we can compare if that string
			// contains "Find a buyer"
			// if so handle it differently than a bill would
			//
			// MailCardMakeDeal and MailCardBill are separate classes
			// and have separate Card ID. ( int Card.getM_cardType() ), and
			// switch on the returned int
			// See Card.MailCardBill and Card.MailCardBuyer.

			// Get ready to draw mail cards
			// We'll use a Vector to hold the bills, and an int to hold buyers.
			Vector<Card> BillPile = new Vector<Card>();
			Days today = Defines.GM.getDayList().elementAt(
					Defines.CP().getDay());
			int numBuyers = 0;

			// Draw a number of cards equal to the mail day's value
			for (int i = 0; i < today.getValue(); i++) {

				// Draw the card
				DrawnCard = Defines.GM.getMailCards().elementAt(
						Defines.GM.rand.nextInt(Defines.GM.getMailCards()
								.size()));

				// Switch on card type
				switch (DrawnCard.getM_cardType()) {
				case Card.MailCardBill:
					// Push the bill onto the HandleDay.Enter bill pile.
					BillPile.add(new MailCardBill(DrawnCard.getName(),
							DrawnCard.getValue(), ((MailCardBill) DrawnCard)
									.getCost()));
					// and onto the player's bill pile.
					Defines.CP().addMailCard((MailCardBill) DrawnCard);
					break;
				case Card.MailCardBuyer:
					// Increment number of buyers
					numBuyers++;

					break;
				default:
					break;
				}
			}

			// Now process the bill pile and the buyers
			if (BillPile.size() > 0) {
				MailCardBillDialog showDialog = new MailCardBillDialog(
						GameFrame.getInstance(), /*(MailCardBill) */BillPile/*.get(0)*/);
				//Defines.CP().addMailCard((MailCardBill) BillPile.get(0));
				//Defines.EVM.queueEvent("EVENT_FINAL_LOAD");
			}
			if ( numBuyers > 0 ){
				SellDealCardDialog sdcd = new SellDealCardDialog(GameFrame.getInstance(),
						numBuyers);
			}
			break;

		}
		case Days.EVENTDAY:
			// Draw an event card
			// Switch on event to call appropriate event dialog
			// Currently, only you pay or you receive events
			// for other events, see
			// http://www.centralconnector.com/GAMES/payday.html
			// 
			// Event Card Types:
			//            take money from player
			//            	take money from player + 1 other player
			//            give money to bonus pile
			//            you + 1 player give money to bonus pile
			//            you get money from bank
			//            you +1 other player get money from bank
			//
			DrawnCard = Defines.GM.getEventCards().elementAt(
							Defines.GM.rand.nextInt(
							Defines.GM.getEventCards().size() ) );
			
			switch( DrawnCard.getM_cardType() ){
			case Card.EventCardGain:
			case Card.EventCardLoss:
				// TODO Call the Eventcard dialog
				EventDayDialog edd = new EventDayDialog(
						GameFrame.getInstance(), DrawnCard );
				break;
			}
			break;
		case Days.DEALDAY: {
			// Look how DrawnCard is typecast as a deal card and passed to
			// DealCardDialog
			// DealCardDialog now has that card and can display its
			// information
			DrawnCard = Defines.GM.getDealCards().elementAt(
					Defines.GM.rand.nextInt(Defines.GM.getDealCards().size()));
			DealCardDialog showDialog = new DealCardDialog(GameFrame
					.getInstance(), (DealCard) DrawnCard);
			
			break;
		}
		case Days.LOTTERYDAY:
			LotteryDayDialog showDialog = new LotteryDayDialog();
			break;
		case Days.BUYERDAY:
			Days today = Defines.GM.getDayList().elementAt(
					Defines.CP().getDay());
			SellDealCardDialog sdcd = new SellDealCardDialog(GameFrame.getInstance(),
					today.getValue());
			break;
		case Days.BIRTHDAY:
			break;
		case Days.YARDSALEDAY:
			break;
		case Days.PAYDAY:
			PayDayDialog showDialog1 = new PayDayDialog(GameFrame.getInstance());
			break;
		case Days.NUM_DAYTYPES:
		default:
			System.out.println("Not a Day ");
			Defines.EVM.queueEvent("EVENT_INITIAL_LOAD");
			return false;
			//break;
		}
		Defines.EVM.queueEvent("EVENT_FINAL_LOAD");
		return false;
	}

	@Override
	public boolean Execute() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean Exit() {
		// TODO Auto-generated method stub
		return false;
	}

}
